Return to start page
Core/General/Struct Stack.j
1 library AStructCoreGeneralStack requires optional ALibraryCoreDebugMisc
2
3 //! textmacro A_STACK takes STRUCTPREFIX, NAME, TYPE
4 $STRUCTPREFIX$ struct $NAME$DataNode
5 //start members
6 private $TYPE$ m_data
7 private thistype m_next
8
9 //start members
10
11 public method data takes nothing returns $TYPE$
12 return this.m_data
13 endmethod
14
15 public method next takes nothing returns thistype
16 return this.m_next
17 endmethod
18
19 //methods
20
21 public static method create takes $TYPE$ data, thistype next returns thistype
22 local thistype this = thistype.allocate()
23 //start members
24 set this.m_data = data
25 set this.m_next = next
26
27 return this
28 endmethod
29 endstruct
30
31 $STRUCTPREFIX$ struct $NAME$
32 //dynamic members
33 private integer m_maxSize
34 //members
35 private $NAME$DataNode m_dataNode
36 private integer m_objects
37
38 //dynamic members
39
40 public method setMaxSize takes integer maxSize returns nothing
41 set this.m_maxSize = maxSize
42 endmethod
43
44 public method maxSize takes nothing returns integer
45 return this.m_maxSize
46 endmethod
47
48 //methods
49
50 /// Adds a new elment to stack.
51 public method push takes thistype data returns nothing
52 local $NAME$DataNode dataNode
53 if (this.m_objects < this.m_maxSize) then
54 set dataNode = $NAME$DataNode.create(data, this.m_dataNode)
55 debug if (dataNode != 0) then
56 set this.m_dataNode = dataNode
57 set this.m_objects = this.m_objects + 1
58 debug else
59 debug call Print("Stack is full - By Jass limit.")
60 debug endif
61 debug else
62 debug call Print("Stack is full - By custom limit.")
63 endif
64 endmethod
65
66 /// Returns the supreme element and removes it from stack.
67 public method pop takes nothing returns integer
68 local integer nodeData = 0
69 local $NAME$DataNode oldDataNode
70 debug if (this.m_dataNode != 0) then
71 set oldDataNode = this.m_dataNode
72 set this.m_dataNode = this.m_dataNode.next()
73 set nodeData = oldDataNode.data()
74 call oldDataNode.destroy()
75 set this.m_objects = this.m_objects - 1
76 debug else
77 debug call Print("Stack is empty.")
78 debug endif
79 return nodeData
80 endmethod
81
82 public static method create takes integer maxSize returns thistype
83 local thistype this = thistype.allocate()
84 //dynamic members
85 set this.m_maxSize = maxSize
86 //members
87 set this.m_dataNode = 0
88 set this.m_objects = 0
89
90 return this
91 endmethod
92 endstruct
93 //! endtextmacro
94
95 /// @todo Remove this after testing it.
96 ///! runtextmacro A_STACK("private", "AIntegerStack", "integer")
97
98 endlibrary